﻿using Domain.Entity;
using Domain.Entity.Fy;
using Domain.Repository;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFCore.Repository
{
	public class EMRRepository : IEMRRepository
	{
		private readonly MyDbContext myDbContext;

		public EMRRepository(MyDbContext myDbContext) 
		{
			this.myDbContext = myDbContext;
		}

		public async Task<bool> AddEMR(EMR mR)
		{
			myDbContext.EMR.Add(mR);
		    int zhi = await myDbContext.SaveChangesAsync();
			return zhi > 0 ? true:false; 
		}

		

		public async Task<bool> DeleteEMR(int id)
		{
			var user = await myDbContext.EMR.SingleOrDefaultAsync(e => e.MreId == id);
			if (user != null) 
			{
				myDbContext.EMR.Remove(user);
				await myDbContext.SaveChangesAsync();
				return true;
			}
			return false;
		}

		

		

		public List<EMR> GetEMR()
		{
			List<EMR> jh = myDbContext.EMR.ToList();
			return jh;
		}

		#region 分页
		

		public EMRFy GetEMRFY(int currentPage, int pageSize)
		{
			List<EMR> eMRs = myDbContext.EMR.Skip((currentPage-1)*pageSize).Take(pageSize).ToList();

			int totalCount = myDbContext.EMR.Count();

			EMRFy result = new EMRFy() { EMRs = eMRs , TotalCount = totalCount };

			return result;
		}

		

		#endregion



		public async Task<bool> UpdateEMR(EMR mR)
		{
			var zhi = await myDbContext.EMR.FindAsync(mR.MreId);
			if(zhi == null)
			{
				return false;
			}
			zhi.EngageType = mR.EngageType;
			zhi.HumanAmount = mR.HumanAmount;
			zhi.Deadline = mR.Deadline;
			zhi.MajorDescribe = mR.MajorDescribe;
			zhi.EngageRequired = mR.EngageRequired;
			zhi.ChangeTime = mR.ChangeTime;
			zhi.Changer = mR.Changer;

			myDbContext.EMR.Update(zhi);
			int xg = await myDbContext.SaveChangesAsync();
			return xg > 0;
		}




	}
}
